package com.lyn.view.progress;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author:longyongning
 * @date:2021/10/2
 * @description:刻度显示控件
 */
public class ScaleView extends View {

    private Paint paint;

    private float scale = 7.7f;

    private int backgroundColor=Color.BLACK;

    private int scaleColor=Color.WHITE;

    private int alarmScaleColor=Color.RED;

    private int progressBarColor=Color.YELLOW;

    public void setScale(float scale) {
        this.scale = scale;
        postInvalidate();
    }

    /**
     * 设置背景颜色
     * @param backgroundColor
     */
    @Override
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        postInvalidate();
    }

    /**
     * 设置刻度条颜色
     * @param scaleColor
     */
    public void setScaleColor(int scaleColor) {
        this.scaleColor = scaleColor;
        postInvalidate();
    }

    /**
     * 设置报警刻度颜色
     * @param alarmScaleColor
     */
    public void setAlarmScaleColor(int alarmScaleColor) {
        this.alarmScaleColor = alarmScaleColor;
        postInvalidate();
    }

    /**
     * 设置进度条颜色
     * @param progressBarColor
     */
    public void setProgressBarColor(int progressBarColor) {
        this.progressBarColor = progressBarColor;
        postInvalidate();
    }

    public ScaleView(Context context) {
        super(context);
        init();
    }

    public ScaleView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(30);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(backgroundColor);

        int width = canvas.getWidth();

        int height = canvas.getHeight();

        int padding = 48;

        int scaleX = width - padding * 2;
        int itemWidth = scaleX / 40;


        int start = 0;
        int end = 0;

        for (int i = 0; i < 41; i++) {
            paint.setColor(scaleColor);
            int higher = 0;
            if (i % 5 == 0) {
                paint.setStrokeWidth(8);
                higher = 33;
                if (i / 5 * 2 == 10) {
                    paint.setColor(alarmScaleColor);
                }
                canvas.drawLine(itemWidth * i + padding + 4, height / 2 + 5, itemWidth * i + padding + 4, height / 2 + higher, paint);
                paint.setColor(scaleColor);
                canvas.drawText((i / 5) * 2 + ".0", itemWidth * i + padding + 4, baseLine(height / 2 + higher + 19), paint);
                if (i == 0) {
                    start = itemWidth * i + padding + 4;
                }
                if (i == 40) {
                    end = itemWidth * i + padding + 4;
                }
            } else {
                higher = 25;
                paint.setStrokeWidth(3);
                canvas.drawLine(itemWidth * i + padding + 8f, height / 2 + 5, itemWidth * i + padding + 8f, height / 2 + higher, paint);
            }
        }

        int lineX = end - start;
        paint.setColor(progressBarColor);
        paint.setStrokeWidth(4);
        canvas.drawLine(padding, height / 2, lineX / 8 * scale + padding + 4, height / 2, paint);

    }


    private float baseLine(float y) {
        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = y + distance;
        return baseline;
    }
}

